package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.BigClientService;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Chinaset;
import com.zhongkai.model3.SetRole;
import com.zhongkai.model3.SetUnit;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class BigClientServiceImpl implements BigClientService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;


    /**
     * 获取全部大客户角色的账号
     */
    @Override
    public ZhongkaiResult bigClientList(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        //SetRole
        SetRole setRole = (SetRole) iBaseDao.get("from SetRole sr where sr.name='大客户'");

        if (setRole == null) {

            return ZhongkaiResult.build(500,"大客户角色不存在");

        } else {


            try {
                PageBean pageBean = new PageBean();
                pageBean.setCurrentPage(page);
                pageBean.setPageSize(rows);
                DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Chinaset.class);
                detachedCriteria.add(Restrictions.eq("setRole", setRole.getId())); //增加条件
                pageBean.setDetachedCriteria(detachedCriteria);
                pageQuerDao.pageQuery(pageBean);
                return ZhongkaiResult.ok(pageBean);
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(405, "查询失败");
            }

        }

    }


    /**
     * 大客户名下所有设备
     *
     * @param user_uuid    大客戶uuid
     */
    @Override
    public ZhongkaiResult bigClientUnitList(Integer page, Integer rows,String user_uuid) {
        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(user_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SetUnit.class);
            detachedCriteria.add(Restrictions.eq("setVolUuid", user_uuid)); //增加条件
            detachedCriteria.add(Restrictions.eq("setBound", "2")); //增加条件 是否绑定业主
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }





    /**
     * 大客户名下所有设备 (902)
     *
     * @param user_uuid    当前ERP用户uuid
     * @param warning    是否报警 0正常  1报警
     */
    @Override
    public ZhongkaiResult bigClientUser(Integer page, Integer rows,String user_uuid,Integer warning) {


        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(user_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }



        Chinaset chinaset = (Chinaset) iBaseDao.get("from Chinaset cs where cs.uuid='" + user_uuid + "'");

        if (chinaset == null){
            return ZhongkaiResult.build(500,"该用户不存在");
        }
        SetRole setRole = (SetRole) iBaseDao.get("from SetRole sr where sr.name='大客户'");

        if (setRole.getId() != chinaset.getSetRole()) {
            return ZhongkaiResult.build(1004,"抱歉,您还不是大客户");
        }
        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SetUnit.class);
            detachedCriteria.add(Restrictions.eq("setVolUuid", user_uuid)); //增加条件
            detachedCriteria.add(Restrictions.eq("setBound", "2")); //增加条件 是否绑定业主
            detachedCriteria.add(Restrictions.eq("warning", warning)); //增加条件 是否报警
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }


    /**
     * 设备搜素
     *
     * @param user_name    设备绑定的客户姓名
     * @param user_uuid    大用户uuid

     */
    @Override
    public ZhongkaiResult searchClientUnit(Integer page, Integer rows, String user_name,String user_uuid) {
        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(user_name) || StringUtils.isNullOrEmpty(user_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SetUnit.class);
            detachedCriteria.add(Restrictions.eq("userName", user_name)); //增加条件
            detachedCriteria.add(Restrictions.eq("setVolUuid", user_uuid)); //增加条件
            detachedCriteria.add(Restrictions.eq("setBound", "2")); //增加条件 是否绑定业主
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }

    }



    /**
     * 根据uuid获取设备详情
     *
     * @param unit_uuid    设备uuid
     */

    @Override
    public ZhongkaiResult getUnitData(String unit_uuid) {
        if ( StringUtils.isNullOrEmpty(unit_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        SetUnit setUnit = (SetUnit) iBaseDao.get("from SetUnit su where su.setUuid='" + unit_uuid + "'");

        if (setUnit == null){
            return ZhongkaiResult.build(500,"设备不存在");
        }
        return ZhongkaiResult.ok(setUnit);
    }
}
